<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">

      <el-form-item label="受检人电话" prop="patentPhone" label-width="150rpx">
        <el-input
          v-model="queryParams.patentPhone"
          placeholder="请输入受检人电话"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>


      <el-form-item label="是否解读" prop="isInterpret">
        <el-select v-model="queryParams.isInterpret" clearable @change="handleQuery">
          <el-option label="是" value="1"></el-option>
          <el-option label="否" value="0"></el-option>
        </el-select>
      </el-form-item>


      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="recordList" @selection-change="handleSelectionChange">
      <el-table-column label="订单编号" align="center" prop="orderNo" />
      <el-table-column label="受检人或者配偶" align="center" prop="patientName" />
      <el-table-column label="受检人电话" align="center" prop="patentPhone" />
      <el-table-column label="检测项目" align="center" prop="sampleDecWay" />
      <el-table-column label="预出报告时间" align="center" prop="reportingDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.reportingDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="出报告时间" align="center" prop="reportDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.reportDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>

      <el-table-column label="报告链接" align="center" prop="reportAddress"
      >
        <template slot-scope="scope">
          <span>
                  <a :href="scope.row.reportAddress" target="_self">
                    <i class="el-icon-download"></i>
                    <span>下载</span>
                  </a>
          </span>
        </template>
      </el-table-column>




      <el-table-column label="预约日期" align="center" prop="subscribeDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.subscribeDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="type"
                       label="预约时间"
                       align="center"
                       width="100">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_appointment_period"
                    :value="scope.row.type" />
        </template>
      </el-table-column>
      <el-table-column label="是否解读" align="center" prop="isInterpret">
        <template slot-scope="scope">
          <span v-if="scope.row.isInterpret === '1'">是</span>
          <span v-else-if="scope.row.isInterpret === '0'">否</span>
        </template>
      </el-table-column>

      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-switch v-model="scope.row.isInterpret"
                     active-value="1"
                     inactive-value="0"
                     @change="handleStatusChange(scope.row)">
          </el-switch>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

  </div>
</template>

<script>
  import {changeInterpretReport, listRecord, getRecord, delRecord, addRecord, updateRecord } from "@/api/system/record/record";

  export default {
    name: "Record",
    dicts: ['sys_appointment_period'],
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 报告记录表格数据
        recordList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          orderNo: null,
          patentPhone: null,
          sampleDecWay: null,
          reportingDate: null,
          reportDate: null,
          reportAddress: null,
          subscribeDate: null,
          subscribeDateId: null,
          type: null,
          subscribeStatus: null,
          isInterpret: null
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
        }
      };
    },
    created() {
      this.getList();
    },
    methods: {


      // 修改是否解读
      handleStatusChange (row) {
        let text = row.isInterpret === "1" ? "解读" : "未解读";
        this.$modal.confirm('确认要"' + text + '"订单号：' + row.orderNo ).then(function () {
          return changeInterpretReport(row.id, row.isInterpret);
        }).then(() => {
          this.$modal.msgSuccess(text + "成功");
        }).catch(function () {
          row.isInterpret = row.isInterpret === "0" ? "1" : "0";
        });
      },

      /** 查询报告记录列表 */
      getList() {
        this.loading = true;
        listRecord(this.queryParams).then(response => {
          this.recordList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
      },

      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length!==1
        this.multiple = !selection.length
      },

      /** 修改按钮操作 */
      handleUpdate(row) {
        const id = row.id || this.ids
        getRecord(id).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "修改报告记录";
        });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.id != null) {
              updateRecord(this.form).then(response => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addRecord(this.form).then(response => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids;
        this.$modal.confirm('是否确认删除报告记录编号为"' + ids + '"的数据项？').then(function() {
          return delRecord(ids);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {});
      },
      /** 导出按钮操作 */
      handleExport() {
        this.download('system/record/export', {
          ...this.queryParams
        }, `record_${new Date().getTime()}.xlsx`)
      }
    }
  };
</script>
